package com.moody.init;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.ReportModelDAO;
import com.moody.hibernate.domain.EnterBalanceSheet;
import com.moody.hibernate.domain.EnterCashFlow;
import com.moody.hibernate.domain.EnterCashFlowGeneration;
import com.moody.hibernate.domain.EnterFinancialIndex;
import com.moody.hibernate.domain.EnterIncome;
import com.moody.hibernate.domain.FinanFinancialIndexSheet;
import com.moody.hibernate.domain.PublicBalanceSheet;
import com.moody.hibernate.domain.PublicFinancialIndex;
import com.moody.hibernate.domain.PublicIncomeSheet;
import com.moody.hibernate.domain.FinanBalanceSheet;
import com.moody.hibernate.domain.FinanIncomeSheet;
import com.moody.hibernate.domain.ReportModel;
import com.moody.hibernate.domain.ReportModelId;
import com.moody.hibernate.domain.SmallBalanceSheet;
import com.moody.hibernate.domain.SmallCashFlowSheet;
import com.moody.hibernate.domain.SmallFinancialIndexSheet;
import com.moody.hibernate.domain.SmallIncomeSheet;

/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-9-16 上午10:43:35
 * @Modified by: 
 * @Description: 数据写入Report Model
 */

public class ReportModelInit {
	private static final Logger log = LoggerFactory
			.getLogger(ReportModelInit.class);
	private static ReportModelInit instance = null;
	static WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	static ServletContext context = web.getServletContext();  
	static WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	private static ReportModelDAO reportModelDAO = ReportModelDAO
			.getFromApplicationContext(ctx);

	/*
	 * 企业法人
	 */
	private static List<ReportModel> enterBalanceSheetList = new LinkedList<ReportModel>();
	private static List<ReportModel> enterBalanceSheetListFromModel = new LinkedList<ReportModel>();
	private static List<ReportModel> enterAssetList = new LinkedList<ReportModel>();
	private static List<ReportModel> enterDebtList = new LinkedList<ReportModel>();
	private static List<ReportModel> enterIncomeList = new LinkedList<ReportModel>();
	private static List<ReportModel> enterCashFlowList = new LinkedList<ReportModel>();
	private static List<ReportModel> enterCashFlowGenerationList = new LinkedList<ReportModel>();
	private static List<ReportModel> enterFinancialIndexList = new LinkedList<ReportModel>();
	/*
	 * 事业单位
	 */
	private static List<ReportModel> publicBalanceSheetList = new LinkedList<ReportModel>();
	private static List<ReportModel> publicBalanceSheetListFromModel = new LinkedList<ReportModel>();
	private static List<ReportModel> publicIncomeList = new LinkedList<ReportModel>();
	private static List<ReportModel> publicIncomeListFromModel = new LinkedList<ReportModel>();
	private static List<ReportModel> publicFinancialIndexList = new LinkedList<ReportModel>();

	/*
	 * 金融同业
	 */
	private static List<ReportModel> finanBalanceSheetList = new LinkedList<ReportModel>();
	private static List<ReportModel> finanBalanceSheetListFromModel = new LinkedList<ReportModel>();
	private static List<ReportModel> finanAssetList = new LinkedList<ReportModel>();
	private static List<ReportModel> finanDebtList = new LinkedList<ReportModel>();
	private static List<ReportModel> finanIncomeList = new LinkedList<ReportModel>();
	private static List<ReportModel> finanFinancialIndexList = new LinkedList<ReportModel>();
	
	/*
	 * 小企业
	 */
	private static List<ReportModel> smallBalanceSheetList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallBalanceSheetListFromModel = new LinkedList<ReportModel>();
	private static List<ReportModel> smallAssetList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallDebtList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallIncomeList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallCashFlowList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallCashFlowGenerationList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallFinancialIndexList = new LinkedList<ReportModel>();
	private static List<ReportModel> smallIncomeListFromModel = new LinkedList<ReportModel>();

	
	private static Map<String, String> displayNoMap = new HashMap<String, String>(); // 通过key查找displayNo
	private static Map<String, String> displayFlagMap = new HashMap<String, String>(); // 通过key查找displayFlag
	private static Map<String, String> displayNameMap = null; // 通过key查找displayName
	private static Map<String, String> modelValueFlagMap = null; //通过可以查找modelValueFlag;

	private ReportModelInit() {

	}

	public static ReportModelInit getInstance() {
		if (instance == null) {
			instance = new ReportModelInit();
			instance.getEnterBalanceSheetList();
			instance.getEnterIncomeList();
			instance.getEnterCashFlowList();
			instance.getPublicBalanceSheetList();
			instance.getPublicIncomeSheetList();
			instance.getFinanBalanceSheetList();
			instance.getFinanIncomeList();
			instance.getFinanFinancialIndexList();
			instance.getSmallBalanceSheetList();
			instance.getSmallIncomeSheetList();
			instance.getSmallFinanceIndexList();
		}
		return instance;
	}
	
	public Map<String,String> getDisplayNameMap(){
		if(displayNameMap != null) return displayNameMap;
		else{
			displayNameMap = new HashMap<String,String>();
			List<ReportModel> list = reportModelDAO.findAll();
			for(ReportModel rm : list){
				displayNameMap.put(rm.getFinanceItemNo(), rm.getDisplayName());
			}
			return displayNameMap;
		}
	}
	public Map<String, String> getModelValueFlagMap(){
		if(modelValueFlagMap != null) return modelValueFlagMap;
		else{
			modelValueFlagMap = new HashMap<String,String>();
			List<ReportModel> list = reportModelDAO.findAll();
			for(ReportModel rm : list){
				modelValueFlagMap.put(rm.getFinanceItemNo(), rm.getModelValueFlag());
			}
			return modelValueFlagMap;
		}
	}

	//获得小企业资产负债表
	public List<ReportModel> getSmallBalanceSheetList(){
		SmallBalanceSheet  obj = new SmallBalanceSheet();
		boolean flag = false; //判断是写入资产还是负债list
		
		if(smallBalanceSheetList!=null && smallBalanceSheetList.size()>0){
			log.info("该model数据已准备完毕");
			return smallBalanceSheetList;
		} else {
			Field[] fields = obj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(obj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 2; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(obj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i-1);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						smallBalanceSheetList.add(rm);
						displayNoMap.put(itemNo, (i-1) + "");
						// 判断是写入资产还是负债list
						if (!flag)
							smallAssetList.add(rm);
						else
							smallDebtList.add(rm);
						if (itemNo.equals("SBS026")) {
							flag = true;
						}
					}
				}
			} else {
				log.info("会企资产负债domain中无数据域");
			}
			// 通过反射机制把会企资产负债表每项写入数据结构
			return smallBalanceSheetList;
		}
	}
	
	//小企业资产表
	public List<ReportModel> getSmallAssetList(){
		if(smallAssetList==null || smallAssetList.size()<=0)
			instance.getSmallBalanceSheetList();
		return smallAssetList;
	}

	
	//小企业负债表
	public List<ReportModel> getSmallDebtList(){
		if(smallDebtList==null || smallDebtList.size()<=0)
			instance.getSmallBalanceSheetList();
		return smallDebtList;
	}

	// 小企业资产负债表批量写入model
	public void insertSmallBalanceSheet() {
		List<ReportModel> list = instance.getSmallBalanceSheetList();
		reportModelDAO.insertReport(list);
	}
	// 小企业现金流量表
	public List<ReportModel> getSmallCashFlowList() {
		SmallCashFlowSheet smallCashFlowObj = new SmallCashFlowSheet();

		if (smallCashFlowList != null && smallCashFlowList.size() > 0) {
			log.info("该model数据已准备完毕");
			return smallCashFlowList;
		} else {
			Field[] fields = smallCashFlowObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(smallCashFlowObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(smallCashFlowObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						smallCashFlowList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("小企业损益domain中无数据域");
			}
			// 通过反射机制把小企业损益表每项写入数据结构
			return smallCashFlowList;
		}
	}

	// 小企业现金流量表批量写入model
	public void insertSmallCashFlowSheet() {
		List<ReportModel> list = instance.getSmallCashFlowList();
		reportModelDAO.insertReport(list);
	}

	
	
	
	
	
	
	
	


	//获得会金融资产负债表
	public List<ReportModel> getFinanBalanceSheetList(){
		FinanBalanceSheet  obj = new FinanBalanceSheet();
		boolean flag = false; //判断是写入资产还是负债list
		
		if(finanBalanceSheetList!=null && finanBalanceSheetList.size()>0){
			log.info("该model数据已准备完毕");
			return finanBalanceSheetList;
		} else {
			Field[] fields = obj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(obj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 2; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(obj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i - 1);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						finanBalanceSheetList.add(rm);
						displayNoMap.put(itemNo, (i - 1) + "");
						// 判断是写入资产还是负债list
						if (!flag)
							finanAssetList.add(rm);
						else
							finanDebtList.add(rm);
						if (itemNo.equals("FBS044")) {
							flag = true;
						}
					}
				}
			} else {
				log.info("会金融资产负债domain中无数据域");
			}
			// 通过反射机制把会金融资产负债表每项写入数据结构
			return finanBalanceSheetList;
		}
	}

	// 快金融资产表
	public List<ReportModel> getFinanAssetList() {
		if (finanAssetList == null || finanAssetList.size() <= 0)
			instance.getFinanBalanceSheetList();

		return finanAssetList;
	}

	// 快金融负债表
	public List<ReportModel> getFinanDebtList() {
		if (finanDebtList == null || finanDebtList.size() <= 0)
			instance.getFinanBalanceSheetList();

		return finanDebtList;
	}

	// 会金融资产负债表批量写入model
	public void insertFinanBalanceSheet() {
		List<ReportModel> list = instance.getFinanBalanceSheetList();
		reportModelDAO.insertReport(list);
	}


	// 会金融损益表
	public List<ReportModel> getFinanIncomeList() {

		if (finanIncomeList != null && finanIncomeList.size() > 0) {
			log.info("该model数据已准备完毕");
			return finanIncomeList;
		} else {
			FinanIncomeSheet finanIncomeObj = new FinanIncomeSheet();
			Field[] fields = finanIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(finanIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(finanIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						finanIncomeList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会金融损益domain中无数据域");
			}
			// 通过反射机制把会金融损益表每项写入数据结构
			return finanIncomeList;
		}
	}

	// 会金融损益表批量写入model
	public void insertFinanIncomeSheet() {
		List<ReportModel> list = instance.getFinanIncomeList();
		reportModelDAO.insertReport(list);
	}

	// 会金融财务比率指标表
	public List<ReportModel> getFinanFinancialIndexList() {

		if (finanFinancialIndexList != null
				&& finanFinancialIndexList.size() > 0) {
			log.info("该model数据已准备完毕");
			return finanFinancialIndexList;
		} else {
			FinanFinancialIndexSheet finanFinancialIndexObj = new FinanFinancialIndexSheet();
			Field[] fields = finanFinancialIndexObj.getClass()
					.getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(finanFinancialIndexObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(finanFinancialIndexObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						finanFinancialIndexList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会金融财务比率指标输入表domain中无数据域");
			}
			// 通过反射机制把会金融财务指标输入表每项写入数据结构
			return finanFinancialIndexList;
		}
	}

	// 会金融财务指标输入表批量写入model
	public void insertFinanFinancialIndexSheet() {
		List<ReportModel> list = instance.getFinanFinancialIndexList();
		reportModelDAO.insertReport(list);
	}

	// 获得会企资产负债表
	public List<ReportModel> getEnterBalanceSheetList() {
		EnterBalanceSheet obj = new EnterBalanceSheet();
		boolean flag = false; // 判断是写入资产还是负债list

		if (enterBalanceSheetList != null && enterBalanceSheetList.size() > 0) {
			log.info("该model数据已准备完毕");
			return enterBalanceSheetList;
		} else {
			Field[] fields = obj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(obj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(obj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						enterBalanceSheetList.add(rm);
						displayNoMap.put(itemNo, i + "");
						// 判断是写入资产还是负债list
						if (!flag)
							enterAssetList.add(rm);
						else
							enterDebtList.add(rm);
						if (itemNo.equals("EBS041")) {
							flag = true;
						}
					}
				}
			} else {
				log.info("会企资产负债domain中无数据域");
			}
			// 通过反射机制把会企资产负债表每项写入数据结构
			return enterBalanceSheetList;
		}
	}

	// 会企资产表
	public List<ReportModel> getEnterAssetList() {
		if (enterAssetList == null || enterAssetList.size() <= 0)
			instance.getEnterBalanceSheetList();
		return enterAssetList;
	}

	// 会企负债表
	public List<ReportModel> getEnterDebtList() {
		if (enterDebtList == null || enterDebtList.size() <= 0)
			instance.getEnterBalanceSheetList();
		return enterDebtList;
	}

	// 会企资产负债表批量写入model
	public void insertEnterBalanceSheet() {
		List<ReportModel> list = instance.getEnterBalanceSheetList();
		reportModelDAO.insertReport(list);
	}

	// 会企损益表
	public List<ReportModel> getEnterIncomeList() {

		if (enterIncomeList != null && enterIncomeList.size() > 0) {
			log.info("该model数据已准备完毕");
			return enterIncomeList;
		} else {
			EnterIncome enterIncomeObj = new EnterIncome();
			Field[] fields = enterIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(enterIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(enterIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						enterIncomeList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把会企损益表每项写入数据结构
			return enterIncomeList;
		}
	}

	// 会企损益表批量写入model
	public void insertEnterIncome() {
		List<ReportModel> list = instance.getEnterIncomeList();
		reportModelDAO.insertReport(list);
	}

	// 现金流量表
	public List<ReportModel> getEnterCashFlowList() {
		EnterCashFlow enterCashFlowObj = new EnterCashFlow();

		if (enterCashFlowList != null && enterCashFlowList.size() > 0) {
			log.info("该model数据已准备完毕");
			return enterCashFlowList;
		} else {
			Field[] fields = enterCashFlowObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(enterCashFlowObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(enterCashFlowObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						enterCashFlowList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把会企损益表每项写入数据结构
			return enterCashFlowList;
		}
	}

	// 现金流量表批量写入model
	public void insertEnterCashFlow() {
		List<ReportModel> list = instance.getEnterCashFlowList();
		reportModelDAO.insertReport(list);
	}

	// 通过ID查编号
	public String getDisplayNoById(String id) {
		// log.info("id: "+map.get(id));
		return displayNoMap.get(id);
	}

	// 财务指标表
	public List<ReportModel> getEnterFinanceIndexList() {

		if (enterFinancialIndexList != null
				&& enterFinancialIndexList.size() > 0) {
			log.info("该model数据已准备完毕");
			return enterFinancialIndexList;
		} else {
			EnterFinancialIndex enterIncomeObj = new EnterFinancialIndex();
			Field[] fields = enterIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(enterIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(enterIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						// rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_COMMON);
						enterFinancialIndexList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把财务指标表每项写入数据结构
			return enterFinancialIndexList;
		}
	}

	// 财务指标批量写入model
	public void insertEnterFinancialIndex() {
		List<ReportModel> list = instance.getEnterFinanceIndexList();
		reportModelDAO.insertReport(list);
	}

	// 现金流量表 自动生成
	public List<ReportModel> getEnterCashFlowGenerationList() {

		if (enterCashFlowGenerationList != null
				&& enterCashFlowGenerationList.size() > 0) {
			log.info("该model数据已准备完毕");
			return enterCashFlowGenerationList;
		} else {
			EnterCashFlowGeneration enterIncomeObj = new EnterCashFlowGeneration();
			Field[] fields = enterIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(enterIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(enterIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						// rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_COMMON);
						enterCashFlowGenerationList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把现金流量表(自动生成)每项写入数据结构
			return enterCashFlowGenerationList;
		}
	}

	// 现金流量表（自动生成）批量写入model
	public void insertEnterCashFlowGeneration() {
		List<ReportModel> list = instance.getEnterCashFlowGenerationList();
		reportModelDAO.insertReport(list);
	}

	/*
	 * 事业单位 资产负债表
	 */
	public List<ReportModel> getPublicBalanceSheetList() {

		if (publicBalanceSheetList != null && publicBalanceSheetList.size() > 0) {
			log.info("该model数据已准备完毕");
			return publicBalanceSheetList;
		} else {
			PublicBalanceSheet publicBalanceSheetObj = new PublicBalanceSheet();
			Field[] fields = publicBalanceSheetObj.getClass()
					.getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(publicBalanceSheetObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 2; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(publicBalanceSheetObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i - 1);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						publicBalanceSheetList.add(rm);
						displayNoMap.put(itemNo, i - 1 + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把现金流量表(自动生成)每项写入数据结构
			return publicBalanceSheetList;
		}
	}

	// 资产
	public List<ReportModel> getPublicAssetFromModel() {
		return this.getPublicBalanceSheetFromModel().subList(0,
				PublicBalanceSheet.FLAG);
	}

	// 负债
	public List<ReportModel> getPublicDebtFromModel() {
		return getPublicBalanceSheetFromModel().subList(
				PublicBalanceSheet.FLAG,
				getPublicBalanceSheetFromModel().size());
	}

	// 现金流量表（自动生成）批量写入model
	public void insertPublicBalanceSheet() {
		List<ReportModel> list = instance.getPublicBalanceSheetList();
		reportModelDAO.insertReport(list);
	}

	// 从数据库reportModel获得事业单位资产负债表
	public List<ReportModel> getPublicBalanceSheetFromModel() {
		if (publicBalanceSheetListFromModel.size() > 0)
			return publicBalanceSheetListFromModel;
		else {
			publicBalanceSheetListFromModel = reportModelDAO
					.findByReportNo(PublicBalanceSheet.REPORTNO);
			return publicBalanceSheetListFromModel;
		}
	}

	/*
	 * 事业单位 收入支出表
	 */
	public List<ReportModel> getPublicIncomeSheetList() {

		if (publicIncomeList != null && publicIncomeList.size() > 0) {
			log.info("该model数据已准备完毕");
			return publicIncomeList;
		} else {
			PublicIncomeSheet publicIncomeObj = new PublicIncomeSheet();
			Field[] fields = publicIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(publicIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 3; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(publicIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i - 2);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						publicIncomeList.add(rm);
						displayNoMap.put(itemNo, i - 2 + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把现金流量表(自动生成)每项写入数据结构
			return publicIncomeList;
		}
	}

	// 现金流量表（自动生成）批量写入model
	public void insertPublicIncome() {
		List<ReportModel> list = instance.getPublicIncomeSheetList();
		reportModelDAO.insertReport(list);
	}

	// 从数据库reportModel获得事业单位 收入支出表
	public List<ReportModel> getPublicIncomeFromModel() {
		if (publicIncomeListFromModel.size() > 0)
			return publicIncomeListFromModel;
		else {
			publicIncomeListFromModel = reportModelDAO
					.findByReportNo(PublicIncomeSheet.REPORTNO);
			return publicIncomeListFromModel;
		}
	}

	// 事业单位 财务指标表
	public List<ReportModel> getPublicFinanceIndexList() {

		if (publicFinancialIndexList != null
				&& publicFinancialIndexList.size() > 0) {
			log.info("该model数据已准备完毕");
			return publicFinancialIndexList;
		} else {
			PublicFinancialIndex publicIncomeObj = new PublicFinancialIndex();
			Field[] fields = publicIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(publicIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(publicIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						// rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_COMMON);
						publicFinancialIndexList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把财务指标表每项写入数据结构
			return publicFinancialIndexList;
		}
	}

	// 财务指标批量写入model
	public void insertPublicFinancialIndex() {
		List<ReportModel> list = instance.getPublicFinanceIndexList();
		reportModelDAO.insertReport(list);
	}

	/*
	 * 小企业 利润表
	 */
	public List<ReportModel> getSmallIncomeSheetList() {
		if (smallIncomeList != null && smallIncomeList.size() > 0) {
			log.info("该model数据已准备完毕");
			return smallIncomeList;
		} else {
			SmallIncomeSheet smallIncomeObj = new SmallIncomeSheet();
			Field[] fields = smallIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(smallIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(smallIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						smallIncomeList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把现金流量表(自动生成)每项写入数据结构
			return smallIncomeList;
		}
	}

	// 小企业 利润表 批量写入model
	public void insertSmallIncome() {
		List<ReportModel> list = instance.getSmallIncomeSheetList();
		reportModelDAO.insertReport(list);
	}

	// 从数据库reportModel获得小企业 利润表
	public List<ReportModel> getSmallIncomeFromModel() {
		if (smallIncomeListFromModel.size() > 0)
			return smallIncomeListFromModel;
		else {
			smallIncomeListFromModel = reportModelDAO
					.findByReportNo(SmallIncomeSheet.REPORTNO);
			return smallIncomeListFromModel;
		}
	}

	// 事业单位 财务指标表
	public List<ReportModel> getSmallFinanceIndexList() {

		if (smallFinancialIndexList != null
				&& smallFinancialIndexList.size() > 0) {
			log.info("该model数据已准备完毕");
			return smallFinancialIndexList;
		} else {
			SmallFinancialIndexSheet smallIncomeObj = new SmallFinancialIndexSheet();
			Field[] fields = smallIncomeObj.getClass().getDeclaredFields();

			if (fields.length > 0) {
				String reportNo = "";
				try {
					reportNo = fields[0].get(smallIncomeObj).toString();
				} catch (Exception e) {
					log.info("模型编号error", e);
				}

				// 遍历所有的属性域
				for (int i = 1; i < fields.length; i++) {
					ReportModel rm = new ReportModel();
					ReportModelId rmi = new ReportModelId();
					Field field = fields[i];
					String itemNo = "";

					try {
						itemNo = field.get(smallIncomeObj).toString();
					} catch (Exception e) {
						log.info("模型item error", e);
					}

					if (!itemNo.equals("")) {
						rmi.setReportNo(reportNo);
						rmi.setDisplayNo(i);
						rm.setId(rmi);
						rm.setFinanceItemNo(itemNo);
						rm.setDisplayName(ReportItemInit.getInstance()
								.getReportItem().get(itemNo));
						rm.setDisplayFlag(ReportModel.DISPLAY_FLAG_TERM);
						smallFinancialIndexList.add(rm);
						displayNoMap.put(itemNo, i + "");
					}
				}
			} else {
				log.info("会企损益domain中无数据域");
			}
			// 通过反射机制把财务指标表每项写入数据结构
			return smallFinancialIndexList;
		}
	}

	// 财务指标批量写入model
	public void insertSmallFinancialIndex() {
		List<ReportModel> list = instance.getSmallFinanceIndexList();
		reportModelDAO.insertReport(list);
	}

	// 收入
	public List<ReportModel> getPublicRevenueFromModel() {
		return this.getPublicIncomeFromModel().subList(0,
				PublicIncomeSheet.FLAG1);
	}

	// 支出
	public List<ReportModel> getPublicExpendFromModel() {
		return getPublicIncomeFromModel().subList(PublicIncomeSheet.FLAG1,
				PublicIncomeSheet.FLAG2);
	}

	// 结余
	public List<ReportModel> getPublicSurplusFromModel() {
		return getPublicIncomeFromModel().subList(PublicIncomeSheet.FLAG2,
				getPublicIncomeFromModel().size());
	}

	// 从数据库reportModel获得会企资产负债表
	public List<ReportModel> getEnterBalanceSheetListFromModel() {
		if (enterBalanceSheetListFromModel.size() > 0)
			return enterBalanceSheetListFromModel;
		else {
			enterBalanceSheetListFromModel = reportModelDAO
					.findByReportNo(EnterBalanceSheet.REPORTNO);
			return reportModelDAO.findByReportNo(EnterBalanceSheet.REPORTNO);
		}
	}

	// 会企资产
	public List<ReportModel> getEnterAssetFromModel() {
		return getEnterBalanceSheetListFromModel().subList(0,
				EnterBalanceSheet.ORDER);
	}

	// 会企负债
	public List<ReportModel> getEnterDebtFromModel() {
		return getEnterBalanceSheetListFromModel().subList(
				EnterBalanceSheet.ORDER,
				getEnterBalanceSheetListFromModel().size());
	}

	//从数据库reportModel获得小企业资产负债表
	public List<ReportModel> getSmallBalanceSheetListFromModel(){
		if(smallBalanceSheetListFromModel.size() > 0)
			return smallBalanceSheetListFromModel;
		else{
			smallBalanceSheetListFromModel = reportModelDAO.findByReportNo(SmallBalanceSheet.REPORTNO);
			return reportModelDAO.findByReportNo(SmallBalanceSheet.REPORTNO);
		}
	}

	
	//小企业资产
	public List<ReportModel> getSmallAssetFromModel(){
		return getSmallBalanceSheetListFromModel().subList(0, SmallBalanceSheet.FLAG);
	}

	//小企业负债
	public List<ReportModel> getSmallDebtFromModel(){
		return getSmallBalanceSheetListFromModel().subList(SmallBalanceSheet.FLAG,getSmallBalanceSheetListFromModel().size());
	}
	// 从数据库reportModel获得小企业现金流量表
	public List<ReportModel> getSmallCashFlowFromModel() {
		return reportModelDAO.findByReportNo(SmallCashFlowSheet.REPORTNO);
	}
	
	

	//从数据库reportModel获得会金融资产负债表
	public List<ReportModel> getFinanBalanceSheetListFromModel(){
		if(finanBalanceSheetListFromModel.size() > 0)

			return finanBalanceSheetListFromModel;
		else {
			finanBalanceSheetListFromModel = reportModelDAO
					.findByReportNo(FinanBalanceSheet.REPORTNO);
			return reportModelDAO.findByReportNo(FinanBalanceSheet.REPORTNO);
		}
	}

	// 会金融资产
	public List<ReportModel> getFinanAssetFromModel() {
		return getFinanBalanceSheetListFromModel().subList(0,
				FinanBalanceSheet.FLAG);
	}

	// 会金融负债
	public List<ReportModel> getFinanDebtFromModel() {
		return getFinanBalanceSheetListFromModel().subList(
				FinanBalanceSheet.FLAG,
				getFinanBalanceSheetListFromModel().size());
	}

	// 从数据库reportModel获得会金融利润表
	public List<ReportModel> getFinanIncomeFromModel() {
		return reportModelDAO.findByReportNo(FinanIncomeSheet.REPORTNO);
	}

	// 从数据库reportModel获得会金融财务比率指标表
	public List<ReportModel> getFinanFinancialIndexFromModel() {
		return reportModelDAO.findByReportNo(FinanFinancialIndexSheet.REPORTNO);
	}

	// 从数据库reportModel获得会金融财务比率指标输入表
	public List<ReportModel> getFinanFinancialIndexInputFromModel() {
		List<ReportModel> listOfAll = reportModelDAO
				.findByReportNo(FinanFinancialIndexSheet.REPORTNO);
		List<ReportModel> listOfInput = new ArrayList<ReportModel>();

		for (int i = 0; i < listOfAll.size(); i++) {
			ReportModel modelItem = listOfAll.get(i);
			if (modelItem.getDisplayFlag().substring(1).equals("1")) {
				listOfInput.add(modelItem);
			}
		}
		return listOfInput;
	}

	// 从数据库reportModel获得会企利润表
	public List<ReportModel> getEnterIncomeFromModel() {
		return reportModelDAO.findByReportNo(EnterIncome.REPORTNO);
	}

	// 从数据库reportModel获得会企现金流量表
	public List<ReportModel> getEnterCashFlowFromModel() {
		return reportModelDAO.findByReportNo(EnterCashFlow.REPORTNO);
	}

	// 从数据库reportModel获得会企财务指标表
	public List<ReportModel> getEnterFinancialIndexFromModel() {
		return reportModelDAO.findByReportNo(EnterFinancialIndex.REPORTNO);
	}

	// 从数据库reportModel获得会事企财务指标表
	public List<ReportModel> getPublicFinancialIndexFromModel() {
		return reportModelDAO.findByReportNo(PublicFinancialIndex.REPORTNO);
	}
	
	// 从数据库reportModel获得会事企财务指标表
	public List<ReportModel> getSmallFinancialIndexFromModel() {
		return reportModelDAO.findByReportNo(SmallFinancialIndexSheet.REPORTNO);
	}

	// 获得财务指标表项和编号
	public List getEnterFinancialIndexItemFromModel() {

		List<Map<String, String>> indexItemList = new ArrayList<Map<String, String>>();

		List<ReportModel> enterFinancialIndexList = reportModelDAO
				.findByReportNo(EnterFinancialIndex.REPORTNO);

		for (ReportModel rm : enterFinancialIndexList) {
			Map<String, String> indexItemMap = new HashMap<String, String>();
			indexItemMap.put("code", rm.getFinanceItemNo());
			indexItemMap.put("name", rm.getDisplayName());
			indexItemList.add(indexItemMap);
		}
		return indexItemList;
	}

	// 获得财务指标表项和编号
	public Map getEnterFinancialIndexItemMapFromModel() {
		List<ReportModel> enterFinancialIndexList = reportModelDAO
				.findByReportNo(EnterFinancialIndex.REPORTNO);
		Map<String, String> indexItemMap = new HashMap<String, String>();
		for (ReportModel rm : enterFinancialIndexList) {
			indexItemMap.put("code", rm.getFinanceItemNo());
			indexItemMap.put("name", rm.getDisplayName());
		}
		return indexItemMap;
	}

	// 所有的项编号和displayFlag放入map便于查找
	@SuppressWarnings("unchecked")
	public Map<String, String> getDisplayFlagMap() {
		if (displayFlagMap.size() > 0)
			return displayFlagMap;
		else {
			List<ReportModel> list = reportModelDAO.findAll();
			for (ReportModel rm : list) {
				displayFlagMap.put(rm.getFinanceItemNo(), rm.getDisplayFlag());
			}
			return displayFlagMap;
		}
	}

	// 通过ID查DisplayFlagS
	public String getDisplayFlagById(String id) {
		if (displayFlagMap.size() <= 0)
			displayFlagMap = getDisplayFlagMap();
		if (displayFlagMap.get(id) == null || displayFlagMap.get(id).equals(""))
			log.info("id为" + id + "的项不在displayFlagMap中");
		return displayFlagMap.get(id);
	}

	public static void main(String[] args) {
		ReportModelInit.getInstance();
	}
}
